/**************************************************************************
 *         Copyright(c) 2008 by Sunplus mMedia  Technology Co., Ltd.      *
 *                                                                        *
 *  This software is copyrighted by and is the property of Sunplus mMedia *
 *  Technology Co., Ltd. All rights are reserved by Sunplus mMedia Techno-*
 *  logy Co., Ltd. This software may only be used in accordance with the  *
 *  corresponding license agreement. Any unauthorized use, duplication,   *
 *  distribution, or disclosure of this software is expressly forbidden.  *
 *                                                                        *
 *  This Copyright notice "M U S T" not be removed or modified without    *
 *  prior written consent of Sunplus mMedia Technology  Co., Ltd.         *
 *                                                                        *
 *  Sunplus mMedia Technology Co., Ltd. reserves the right to modify this *
 *  software without notice.                                              *
 *                                                                        *
 *  Sunplus mMedia Technology Co., Ltd.                                   *
 *  19-1, Innovation First Road, Science-Based Industrial Park,           *
 *  Hsin-Chu, Taiwan, R.O.C.                                              *
 *                                                                        *
 *  Author: Ted Chiu                                                      *
 **************************************************************************/
#include "general.h"
#include "disp_api.h"
#include "panel.h"

UINT8 HAL_Wait(UINT32 WaitTime);

enum {
	FD54124B_RGB444	= 3,
	FD54124B_RGB565	= 5,
	FD54124B_RGB666	= 6
};

void FD54124B_Init(UINT8 LCM_mode) 
{
	//UINT16 i;
	UINT16 para[4];
	UINT8  FD54124B_mode;
	dispPreInit();
	XBYTE[0x2028] = 48 - 1;	// Set LCM dispnxck freq (0:1x, 1:2x, 2:3x, 3:4x...)
	XBYTE[0x2029] = 0x00;	// Set LCM disp1xck freq (0:1x, 1:2x, 2:3x, 3:4x...)
	XBYTE[0x2D92] = 0x03;               // A0 go high for standby mode, CS0 inactive(high)
	XBYTE[0x2D00] = 0x0a;               // tvdspmode LCM
	XBYTE[0X2D06] = 1;	//vsync width
	XBYTE[0X2D07] = 1;	//hsync width
	dispPnlVhSet(128+20-1, 160+100, 10, 3, 128, 160);
	XBYTE[0x2D90] = 3; 	// actpulse: XWR active width
	XBYTE[0x2D91] = 3;	// rcvpulse: XRD active width
	XBYTE[0X2D27] = 8;	//ups_ck_phase

/* set LCM format */
	if(LCM_mode==LCM_8BITS_RGB444) {
		FD54124B_mode = FD54124B_RGB444;
		LCM_InitSet(8,LCM_RGB444);
		XBYTE[0x2D8A] = 0;	//LCM_DATA_ALIGNED_NONE
		XBYTE[0x2D8B] = 1;	/* {R,G,B} */
		XBYTE[0x2D2B] |= 0x02;	//dither enable
	}
	else if(LCM_mode==LCM_8BITS_RGB565) {
		FD54124B_mode = FD54124B_RGB565;
		LCM_InitSet(8,LCM_RGB565);
		XBYTE[0x2D8A] = 0;	//LCM_DATA_ALIGNED_NONE
		XBYTE[0x2D8B] = 3;	/* {R,G,B}, switch G[7:0] */
	}
	else if(LCM_mode==LCM_8BITS_RGB666) {
		FD54124B_mode = FD54124B_RGB666;
		LCM_InitSet(8,LCM_RGB666);
		XBYTE[0x2D8A] = 1;
		XBYTE[0x2D8B] = 1;	/* {R,G,B} */
	}
	else if(LCM_mode==LCM_9BITS_RGB666) {
		FD54124B_mode = FD54124B_RGB666;
		LCM_InitSet(9,LCM_RGB666);
		XBYTE[0x2D8A] = 0;
		XBYTE[0x2D8B] = 3;	/* {R,G,B} */
	}
	#if	0	//not available on curr package of chip
	else if(LCM_mode==LCM_16BITS_XRGB444) {
		FD54124B_mode = FD54124B_RGB444;
		LCM_InitSet(16,LCM_RGB444);
		XBYTE[0x2D8A] = 0;
		XBYTE[0x2D8B] = 1;	/* {R,G,B} */
	}
	else if(LCM_mode==LCM_16BITS_RGB565) {
		FD54124B_mode = FD54124B_RGB565;
		LCM_InitSet(16,LCM_RGB565);
		XBYTE[0x2D8A] = 0;
		XBYTE[0x2D8B] = 1;	/* {R,G,B} */
	}
	else if(LCM_mode==LCM_16BITS_RDGDBD666) {
		FD54124B_mode = FD54124B_RGB666;
		LCM_InitSet(16,LCM_RGB666);
		XBYTE[0x2D8A] = 3;	//LCM_DATA_ALIGNED_BOTH
		XBYTE[0x2D8B] = 1;	/* {R,G,B} */
	}
	else if(LCM_mode==LCM_18BITS_RGB666) {
		FD54124B_mode = FD54124B_RGB666;
		LCM_InitSet(18,LCM_RGB666);
		XBYTE[0x2D8A] = 0;	//LCM_DATA_ALIGNED_NONE
		XBYTE[0x2D8B] = 1;	/* {R,G,B} */
	}
	#endif
	XBYTE[0x2D29] = 0x01;	// Update display registers
	XBYTE[0x2D25] = 0x01;	// Display controller enable

// FD54124B Init
	/* send LCM cmd */
	XBYTE[0x2D85] = 0x01;               // LCM command mode enable
	XBYTE[0x2D92] = 0x00;               // A0 go low for command mode, CS0 active(low)
	dispLcmWrite(0x01, NULL, 0);	//SW reset
  	HAL_Wait(5);	//delay 5ms

	dispLcmWrite(0x11, NULL, 0);
	dispLcmWrite(0x20, NULL, 0);
	//LCM_Wrt(0x38, NULL, 0);
	dispLcmWrite(0x13, NULL, 0);
	dispLcmWrite(0x11, NULL, 0);
	para[0] = FD54124B_mode;
	dispLcmWrite(0x3A, para, 1);

	para[0] = 0;
	para[1] = 0;
	para[2] = 0;
	para[3] = 160-1;
	dispLcmWrite(0x2B, para, 4);

	para[0] = 0;
	para[1] = 0;
	para[2] = 0;
	para[3] = 128-1;
	dispLcmWrite(0x2A, para, 4);

	//para[0] = 0;
	dispLcmWrite(0xB4, para, 1);
	HAL_Wait(500);	//delay 500ms
	para[0] = 0xC8;
	dispLcmWrite(0xC5, para, 1);

	para[0] = 0x03;
	dispLcmWrite(0xC0, para, 1);

	para[0] = 0x1F;
	dispLcmWrite(0xC6, para, 1);

	para[0] = 0x00;
	para[0] = 0x04;

	dispLcmWrite(0xFE, para, 2);

	HAL_Wait(500);	//delay 500ms
	dispLcmWrite(0x29, NULL, 0);
	dispLcmWrite(0x2C, NULL, 0);

	/* set LCM format */
	XBYTE[0x2D85] = 0x00;               // LCM command mode disable
	XBYTE[0x2D92] = 0x02;               // A0 go high for data mode, CS0 active(keep low)

	dispVsyncWait(DISP_SIGNAL_RISING);

	XBYTE[0x2D93] = 0x01;				// dmaflag

	//XBYTE[0x2D29] = 0x01;	// Update display registers
	XBYTE[0x2D25] = 0x01;	// Display controller enable
	XBYTE[0x27E4] = 0x02;	// Update display registers
}


